﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WGMClient.WGMSvc;
using System.ServiceModel;
using WGMClient.HelperClasses;
using System.Threading;
using System.Windows.Media;
using System.Windows;

namespace WGMClient.Core
{
    
    public class ClientEngine
    {
        InstanceContext _instanceContext;
        WGMSvc.ServiceSoapClient _wgmSvc;
        public WGMSvc.ServiceSoapClient WGMSvcClient { get { return _wgmSvc; } }
        bool _connectionFaulted = false;
        public ClientEngine()
        {
            HelperClasses.Registry.Engine = this;
            _instanceContext = new InstanceContext(this);
            _wgmSvc = new ServiceSoapClient();
            
        }
        #region ServerCalls
        public void HandleClientMessage(WGMMessage message)
        {
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                    if(message != null)
                        Registry.NotifyListeners(message);
                }
            );
        }

        public void Ping()
        {

        }
        #endregion


        internal void SendMessageToServer(WGMMessage message)
        {
            ThreadPool.QueueUserWorkItem
            (
                delegate 
                {
                    try
                    {
                        message.SenderLogon = HelperClasses.Registry.Username;
                        message.SenderMacAddress = Registry.MacAddress;
                        message.SenderTimeStamp = DateTime.Now;
                        message.SenderVersion = Registry.CurrentVersion;
                        if (_wgmSvc.State == CommunicationState.Faulted)
                            _wgmSvc = new ServiceSoapClient();
                        var response = _wgmSvc.HandleServerMessage(message);
                        
                        if(_connectionFaulted)
                        {
                            HelperClasses.Registry.MainWindow.ClearDropDownMessage("_connectionnotification_");

                        }
                        HelperClasses.Registry.NotifyListeners(new LocalMessage() { MessageType = LocalMessageTypes.CONNECTION_RESTORED });
                        _connectionFaulted = false;

                        HandleClientMessage(response);
                    }
                    catch(Exception ex)
                    {
                        if(!_connectionFaulted)
                            HelperClasses.Registry.MainWindow.SetDropDownMessage("Connection issues...", Colors.White, Colors.OrangeRed,"_connectionnotification_");
                        _connectionFaulted = true;
                        HelperClasses.Registry.NotifyListeners(new LocalMessage() { MessageType = LocalMessageTypes.CONNECTION_FAULTED, MessageData = message });
                        if (message.MessageType == MessageTypes.USER_REQUEST_PROFILE_IMAGE || message.MessageType == MessageTypes.USER_REQUEST_PROFILE)
                        {
                            Thread.Sleep(2000);
                            SendMessageToServer(message);
                        }
                        HelperFunctions.LogException(ex);
                    }
                }                
            );
        }
    }
}
